// pages/index/search/search.js
Component({
  data: {
    list: [],	//这是搜索到的结果
    list2: [],	//这是所有可供查询的记录
    hotlist:[{name:"6666"}],
    history:["整租","月租"],
    value: '',
    resultList: [],
    focus:false,
    inputValue:"",
    firstinputValue:""
  },

  methods: {
    hotDirect(options){
      console.log(options)
      wx.request({
        url: 'http://localhost:8080/ihome/home/list',
        method: 'GET',
        header: {
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        data:{},
        success:(resp)=>{
          console.log(resp)
          this.setData({
            list:resp.data.rows
          })
        }
      })
        console.log(e)
        var list = this.data.list;	
        var list2 = [];	
        //循环去取数据
        for(var i=0;i<list.length;i++){
          var string = list[i].name;
          if(string.indexOf(e.detail.value) >= 0){
            list2.push(list[i]);
          }
        }
        if(e.detail.value == ""){
          this.setData({
            hotlist: this.data.hotlist
          })
        } else {
          this.setData({
            hotlist: list2
          })
        }
    },
    searchtohome(e){
      console.log(e)
      wx.navigateTo({
        url: '/pages/index/home/home',
      })
    },

    focusHandler(e){
      this.setData({inputValue:e.detail.value});
      this.setData({focus:true});
    },
    //叉号
    cancelHandler(e)
    {
      this.setData({inputValue:""})
      this.setData({focus:false});
      this.query
    },
    inputBlur(e){
      this.setData({focus:false});
    },
    //回车搜索并添加新历史
    confirmTap(e) {
      console.log(e)
      wx.navigateTo({
        url: '/pages/index/monthlyRent/monthlyRent',
      })
      var newhistory=[e.detail.value]
      console.log(newhistory)
      this.setData({
        history: newhistory.concat(this.data.history)
      })
      console.log(this.data.history)
      },
  //input动态获取数据
    query(e){
      var name = e.currentTarget.dataset.name;
      if(this.data.firstinputValue!=""){
      this.setData({
        [name]: e.detail.value.replace(/\s+/g, ''),
        firstinputValue:""
      })
    }else{
      
      this.setData({
        inputValue:e.detail.value,
      })
    }
    wx.request({
      url: 'http://localhost:8080/ihome/home/list',
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      data:{
        houseName:this.data.inputValue
      },
      success:(resp)=>{
        console.log(wx.getStorageSync('token')),
        console.log(resp.data)
        this.setData({
          list:resp.data.rows
        })
      }
    })
  //   this.apper
  // },
  // apper(e){
        //首先回显输入的字符串
        //实现搜索的功能
        console.log(e)
      var list = this.data.list;	
      var list2 = [];	
      //循环去取数据
      for(var i=0;i<list.length;i++){
        var string = list[i].name;
        if(e.detail.value!=null){
        if(string.indexOf(e.detail.value) >= 0){
          list2.push(list[i]);
        }
      }
      }
      if(e.detail.value == ""){
        this.setData({
          list: this.data.hotlist
        })
      } else {
        this.setData({
          list: list2
        })
      }

    },
    //历史记录删除
    handleClose:function(e) {
      console.log(e)
      const index = e.currentTarget.dataset.index; 
    if (index === undefined) {
      console.error('索引为空');
      return;
    }
    const { history } = this.data;
    history.splice(index, 1); // 从数组中移除对应的项
    this.setData({
      history:history
    });
  },
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.hotDirect(options)
    // this.query
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})